package com.SpringBoot.order.service;

import com.SpringBoot.Util.Pager;
import com.SpringBoot.Util.Result;
import com.SpringBoot.order.dto.OrderDto;
import com.SpringBoot.order.mapper.OrderMapper;
import com.SpringBoot.order.pojo.Order;
import com.SpringBoot.orderDetail.mapper.OrderDetailMapper;
import com.SpringBoot.orderDetail.pojo.OrderDetail;
import com.SpringBoot.user.pojo.User;
import com.SpringBoot.user.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service
public class OrderService {

    private final OrderMapper orderMapper;
    private final OrderDetailMapper orderDetailMapper;
    private final UserService userService;

    public OrderService(OrderMapper orderMapper, OrderDetailMapper orderDetailMapper, UserService userService) {
        this.orderMapper = orderMapper;
        this.orderDetailMapper = orderDetailMapper;
        this.userService = userService;
    }

    /**
     * 添加订单 + 订单详情（事务保证一致性）
     */
    @Transactional
    public Result addOrderWithDetails(Order order, List<OrderDetail> details) {

        // 1. 新增订单
        orderMapper.addOrder(order);

        // 此时 order.getOrderId() 已经有值（useGeneratedKeys="true"）
        Integer orderId = order.getOrderId();

        // 2. 批量新增订单详情
        for (OrderDetail detail : details) {
            detail.setOrderId(orderId);
            orderDetailMapper.addOrderDetail(detail);
        }

        return Result.success("下单成功");
    }

    /**
     * 删除订单（逻辑删除）+ 同时删除订单明细
     */
    @Transactional
    public Result deleteOrder(Integer orderId) {
        // 删除订单
        orderMapper.deleteOrder(orderId);
        // 删除订单明细
        orderDetailMapper.deleteOrderDetail(orderId);

        return Result.success("订单删除成功");
    }

    /**
     * 查询用户订单是否存在
     */
    public Order selectExistOrder(Integer userId) {
        return orderMapper.selectExistOrder(userId);
    }

    public Result updateByOrderId(Integer orderId,Integer status) {
        orderMapper.updateByOrderId(orderId,status);
        return Result.success("修改成功");
    }

    public Result allAmount(){
        return Result.success(orderMapper.allAmout());
    }

    public Result selectUserOrder(Integer userId) {
        Result result =userService.existUser(userId);
        if (result.getCode() != 0 ){
            return result;
        }
        return Result.success(orderMapper.selectUserOrder(userId));
    }

    public Result selectOrderByOrderId(Integer orderId) {
        return Result.success(orderMapper.selectOrderByOrderId(orderId));
    }

    public Result selectOrderByPage(Integer pageNum, Integer pageSize){
        System.out.println("order:"+pageNum + "-" + pageSize);
        System.out.println(pageSize+" "+ pageNum);
        //查询总记录数
        Integer recordCount = orderDetailMapper.getOrderDetailCount();
        //构建分页信息对象
        Pager pager = new Pager(pageNum, pageSize, recordCount);
        //根据pager查询需要的数据
        List<OrderDto> list = orderMapper.selectOrderByPage(pager.getStart(), pageSize);
        //将分页信息和查询到的数据一起返回到前端
        HashMap<String,Object> map = new HashMap<>();
        map.put("pager",pager);
        map.put("list",list);
        System.out.println(map);
        return Result.success(map);
    }
}
